package net.atomarrow.bs.service;

import java.io.InputStream;
import java.io.Serializable;
import java.util.List;

import net.atomarrow.bean.Pager;
import net.atomarrow.bean.ServiceResult;
import net.atomarrow.bs.domain.TbTopic;
import net.atomarrow.bs.form.TopicBsForm;
import net.atomarrow.db.parser.Conditions;
import net.atomarrow.services.Service;
import net.atomarrow.sy.staticvariable.FM;
import net.atomarrow.util.DateUtil;
import net.atomarrow.util.ExcelUtil;
import net.atomarrow.util.FileUtil;
import net.atomarrow.util.StringUtil;
import net.atomarrow.util.excel.ExcelDatas;
import net.atomarrow.util.excel.ExcelFormatListener;
import net.atomarrow.util.excel.SimpleHSSFCellStyle;

public class TopicBsService extends Service {

    protected static final String domainName=TbTopic.class.getSimpleName();
    
    protected Conditions getConditions() {
        return new Conditions(domainName);
    }
    
    protected int modifyColumnById(Integer id,Serializable... columns) {
        return  modifyWithColumn(getConditions().putEW("id", id), columns);
    }
    
    protected List<TbTopic> getList(Serializable... columns) {
        Conditions conditions=getConditions();
        for(int i=0;i<columns.length;i+=2){
            conditions.putEW(columns[i], columns[i+1]);
        }
        return  getList(conditions);
    }
    
    protected List<TbTopic> getListByPage(Pager pager,Serializable... columns) {
        Conditions conditions=getConditions();
        for(int i=0;i<columns.length;i+=2){
            conditions.putEW(columns[i], columns[i+1]);
        }
        pager.setDataTotal(getCount(conditions));
        return  getListByPage(conditions, pager);
    }
    public ServiceResult addTopic(TbTopic topic){
        // needDo Topic add 逻辑判断处理
//      if (StringUtil.isBlank(topic.getName())) {
//          return error("名称不能为空！");
//      }
//      if(checkExist(TbTopic.class, "name", topic.getName(), topic.getId())){
//          return error("名称已被使用，请更换其他名称！");
//      }
//    	TbTopic topic=new TbTopic();
        topic.setCreateTime(DateUtil.getNow());
//        topic.setNull("chapterCount");
        add(topic);
        System.out.println(topic.getId());
        return SUCCESS;
    
    }
    
    
    public ServiceResult modifyTopic(TbTopic topic){
        // needDo Topic add 逻辑判断处理
//      if (StringUtil.isBlank(topic.getName())) {
//          return error("名称不能为空！");
//      }
//      if(checkExist(domainName, "name", topic.getName(), topic.getId())){
//          return error("名称已被使用，请更换其他名称！");
//      }
    	topic.setNull("img");
        modify(topic);
        return SUCCESS;
    }
    
    
    public ServiceResult delById(Integer id){
        if(id==null){
            return error(FM.ERROR);
        }
        // needDo Topic del 逻辑判断处理
        delById(domainName,id);
        return SUCCESS;
    }
     
    public TbTopic getById(Integer id) {
        return getById(domainName,id);
    }

    
    public List<TbTopic> getAll() {
        return getList(getConditions());
    }
    
    
    public List<TbTopic> getList(TopicBsForm form,Pager pager){
        Conditions conditions = getConditions();
        
        if(form!=null){
        // needDo Topic getList 查询条件添加
            if(StringUtil.isNotBlank(form.getKeywords())){
                conditions.putLIKE("name", form.getKeywords());
            }
        }
        if (pager == null) {
            return this.getList(conditions);
        }
        pager.setDataTotal(this.getCount(conditions));

        return this.getListByPage(conditions, pager);
    }
    
    
    
    public InputStream getExcelModel(String realPath){
        String note = this.getNote();
        SimpleHSSFCellStyle hss = new SimpleHSSFCellStyle();
        hss.setWrapText(true);
        ExcelDatas excelDatas = new ExcelDatas();
        excelDatas.addString(0, ExcelDatas.AUTO, note, hss);
        return ExcelUtil.exportExcelWithHead(realPath,
                excelDatas);
    }
    
    
    public ServiceResult addFromExcel(String filepath) {
        List<TbTopic> topics = null;
        // 解析excel
        try {
            //needDo Topic addFromExcel 字段匹配处理
            topics = ExcelUtil.getListFromExcel(
                    filepath,
                    TbTopic.class,
                    new String[] { "name" },
                    new boolean[] { true}, 1,
                    new ExcelFormatListener() {

                        
                        public Object changeValue(String field, Object value,
                                int row, int col) {
                                
                            //needDo Topic addFromExcel Listener处理
                            return value;
                        }
                    });
        } catch (Throwable e) {
            e.printStackTrace();
            return error(e.getMessage());

        } finally {
            new FileUtil().delFileWithThread(filepath);

        }
        if (topics.size() == 0) {
            return error(FM.NO_DATA_CAN_NOT_ADD);
        }

        ServiceResult result = checkValidity(topics);
        if (result.hasError()) {
            return result;
        }
        addByBatch(topics);
        return SUCCESS;

    }
    
    
    
    public InputStream export(TopicBsForm form) {
        List<TbTopic> topics=this.getList(form,null);
        ExcelDatas excelDatas = new ExcelDatas();
        // needDo Topic export 字段匹配处理
        excelDatas.addStringArray(0, 0, new String[]{"名称"});
        excelDatas.addObjectList(1, 0, topics, new String[] { "name"});
        return ExcelUtil.exportExcel(excelDatas);
    }

    /**
     * excel模板备注信息
     */
    private String getNote() {
        StringBuilder msg=new StringBuilder(100);
        // needDo Topic getExcelModel 模板备注
        return msg.toString();
    }
    
    /**
     * excel导入list校验合法性
     */
    private ServiceResult checkValidity(List<TbTopic>  topics) {
        // needDo Topic addFromExcel 合法性校验
        return SUCCESS;
    }

	public void addExc() {
		Conditions conditions = getConditions();
		conditions.putEW("asdf", 1);
		getList(conditions);
		throw new RuntimeException("343333");
	}


}
